<?php

class SiteController extends ProjectController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	public function actionEmdbed(){
		$this->layout = "blank";
		$c = Yii::app()->request->getParam('c',null);
		$w = Yii::app()->request->getParam('w',500);
		$h = Yii::app()->request->getParam('h',400);
		if($c == null)  $this->redirect('/');
		$channel = Channels::model()->findByAttributes(array(
			'status'=>0,
			'name'=>$c
		));
		if(empty($channel)) $this->redirect('/');
		$rtmp = Channels::model()->getRtmp();
		$this->render('embed',array(
			'channel'=>$channel,
			'w'=>$w,
			'h'=>$h,
			'rtmp'=>$rtmp,
		));
	}
	public function actionIndex(){
		$model = new Channels;
		Yii::app()->session['stream'] = null;
		if(isset($_POST['ajax']) && $_POST['ajax']==='channel')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		$object_id = null;
		$reason = null;
		$type = 0;
		if(Yii::app()->user->isGuest){
			$type = 1;
			
			if(!empty(Yii::app()->session['object_id'])){
				$reason = Yii::app()->session['object_id'];
			}else{
				Yii::app()->session['object_id'] = md5(uniqid(32));
				$reason = Yii::app()->session['object_id'];
			}
		}else{
			$object_id = Yii::app()->user->id;
			
		}
		
		if(!empty($_POST)){
			$model->attributes = $_POST['Channels'];
			if($model->validate()){
				$model->stream_key = md5(uniqid(32));
				$model->object_id = $object_id;
				$model->type = $type;
				$model->reason = $reason;
				$model->created = time();
				if($model->save()){
					Yii::app()->session['stream'] = outStr($model->id);
					$this->redirect('/details');
				}else{
				
				}
			}else{
			
			}
		}
		
		
		$this->render('index',array(
			'model'=>$model
		));
	}
	public function actionWatch(){
		$alias = Yii::app()->request->getParam('alias',null);
		if($alias == null)  $this->redirect('/');
		$channel = Channels::model()->findByAttributes(array(
			'status'=>0,
			'name'=>$alias
		));
		if(empty($channel)) $this->redirect('/');
		
		
		
		$this->render('watch',array(
			'channel'=>$channel,
		
		));
	}
	public function actionBroadcast(){
		$id = Yii::app()->request->getParam('id',null);
		if($id == null)  $this->redirect('/');
		$this->render('broadcast',array(

		));
	}
	public function actionDetails(){
		if(empty(Yii::app()->session['stream']))  $this->redirect('/');
		$settings = Settings::model()->findAll();
		
		$channel = Channels::model()->findByAttributes(array(
			'status'=>0,
			'id'=>outBin(Yii::app()->session['stream'])
		));
		if(empty($channel)) $this->redirect('/');
		
		$this->render('details',array(
			'settings'=>$settings,
			'channel'=>$channel,
		));
	}
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		$this->layout = "error";
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else{
				$this->render('error', $error);
			}
		}
	}

	
	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		// if(isset($_POST['ContactForm']))
		// {
			// $model->attributes=$_POST['ContactForm'];
			// if($model->validate())
			// {
				// $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				// $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				// $headers="From: $name <{$model->email}>\r\n".
					// "Reply-To: {$model->email}\r\n".
					// "MIME-Version: 1.0\r\n".
					// "Content-type: text/plain; charset=UTF-8";

				// mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				// Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				// $this->refresh();
			// }
		// }
		$this->render('contactus',array('model'=>$model));
	}

	
	
}
